<?php
/*
Plugin Name: BPDEV-SUBSCRIBE-WIDGET
Plugin URI: http://buddypressdev.org/projects/#subscribe
Description: Add a widget to subscribe to a blog
Author: Nicola Greco
Version: 0.3
Author URI: http://nicolagreco.com

Copyright (c) 2009-2011, Nicola Greco (mail: notsecurity@gmail.com | website: http://nicolagreco.com).

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

if ( get_site_option( 'bpdev-subscribe-status' ) == "on" ) {

	if ( 'on' == get_site_option( 'bpdev-widgets-status' ) ) {

		add_action( 'widgets_init', 'bpdev_widgets_subscribe_subscribe_init' );
		add_action( 'widgets_init', 'bpdev_widgets_subscribe_subscribers_init' );

	} elseif ( !function_exists( 'bpdev_widgets_setup_globals' ) ) {

		add_action( 'widgets_init', 'bpdev_widgets_subscribe_subscribe_init' );
		add_action( 'widgets_init', 'bpdev_widgets_subscribe_subscribers_init' );

	}

}

function bpdev_widgets_subscribe_subscribe_init() {

	global $bp;

	if ( !function_exists( 'register_sidebar_widget' ) )
		return;

	function bpdev_widgets_subscribe_display( $args ) {

		global $bp, $current_blog;

		extract( $args );

		?>

		<?php echo $before_widget; ?>
		<?php echo $before_title; ?>
			Subscribe To This Blog!
		<?php echo $after_title; 

			if ( $bp->loggedin_user->id > 0 ) {

				if ( 'on' == $_GET['bpdev_subscribe'] ) {

					if ( BPDEV_Subscribe::check( $bp->loggedin_user->id, $current_blog->blog_id ) ) {

						?>

						You've already subscribed this blog!

						<?php

						do_action( 'bpdev_subscribe_deactivation' );


					} else {

						BPDEV_Subscribe::subscribe( $bp->loggedin_user->id, $current_blog->blog_id );

						?>

						Subscription status ON!

						<?php

						do_action( 'bpdev_subscribe_deactivation' );

					}

				} elseif ( 'off' == $_GET['bpdev_subscribe'] ) {

					if ( !BPDEV_Subscribe::check( $bp->loggedin_user->id, $current_blog->blog_id ) ) {

						?>

						You aren't subscribed to this blog!

						<?php

						do_action( 'bpdev_subscribe_activation' );


					} else {

						BPDEV_Subscribe::unsubscribe( $bp->loggedin_user->id, $current_blog->blog_id );

						?>

						Subscription status OFF!

						<?php

						do_action( 'bpdev_subscribe_activation' );

					}

				} else {

					if ( !BPDEV_Subscribe::check( $bp->loggedin_user->id, $current_blog->blog_id ) ) {

						do_action( 'bpdev_subscribe_activation' );

					} else {

						do_action( 'bpdev_subscribe_deactivation' );

					}

				}

			} else {

				do_action( 'bpdev_widgets_subscribe_loggedin_false' );

			}

		echo $after_widget;

		?>

	<?php

	}

	register_sidebar_widget( 'BPDEV Subscribe','bpdev_widgets_subscribe_display' );

}

function bpdev_widgets_subscribe_subscribers_init() {

	global $bp;

	if ( !function_exists( 'register_sidebar_widget' ) )
		return;

	function bpdev_widgets_subscribe_subscribers_display( $args ) {

		global $bp;

		extract( $args );

		?>

		<?php echo $before_widget; ?>
		<?php echo $before_title; ?>
			Subscribed users
		<?php echo $after_title;  ?>

		<?php do_action( 'bpdev_widgets_subscribe_subscribers_show', $avatar, $num ); ?>


		<?php echo $after_widget;

		?>

	<?php

	}

	register_sidebar_widget( 'BPDEV Show Subscribers','bpdev_widgets_subscribe_subscribers_display' );

}

?>